﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using QuanLiNhaSachBUS;
using QuanLiNhaSachDTO;

namespace QuanLiNhaSachGUI
{
    public partial class frmQuanLiTheLoai : Form
    {
        public frmQuanLiTheLoai()
        {
            InitializeComponent();
        }

        private void frmQuanLiTheLoai_Load(object sender, EventArgs e)
        {
            btnCapNhat.Enabled = false;
            btnXoa.Enabled = false;
            lstTheLoai.DataSource = TheLoaiBUS.loadAll();
            lstTheLoai.ValueMember = "id";
            lstTheLoai.DisplayMember = "Ten";
            txtTenTheLoai.Select();
        }

        private void lstTheLoai_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (lstTheLoai.SelectedValue.ToString() == "System.Data.DataRowView")
                return;
            dataGridView.DataSource = TheLoaiBUS.TimSachTuTheLoai(lstTheLoai.SelectedValue.ToString());
            txtMaTheLoai.Text = lstTheLoai.SelectedValue.ToString();
            TheLoai tl = TheLoaiBUS.search(txtMaTheLoai.Text);
            txtTenTheLoai.Text = tl.Ten;
            btnCapNhat.Enabled = true;
            btnXoa.Enabled = true;
            btnLuu.Enabled = false;
        }

        private void txtTenTheLoai_TextChanged(object sender, EventArgs e)
        {

        }

        private void txtTenTheLoai_KeyDown(object sender, KeyEventArgs e)
        {
        }

        private void btnLuu_Click(object sender, EventArgs e)
        {
            TheLoai tl = new TheLoai();
            tl.Ten = txtTenTheLoai.Text;
            TheLoaiBUS.insert(tl);
            lstTheLoai.DataSource = TheLoaiBUS.loadAll();
        }

        private void btnCapNhat_Click(object sender, EventArgs e)
        {
            TheLoai tl = new TheLoai();
            tl.Ten = txtTenTheLoai.Text;
            tl.Id = txtMaTheLoai.Text;
            TheLoaiBUS.update(tl);
            lstTheLoai.DataSource = TheLoaiBUS.loadAll();
        }

        private void btnXoa_Click(object sender, EventArgs e)
        {
            TheLoaiBUS.delete(txtMaTheLoai.Text);
            lstTheLoai.DataSource = TheLoaiBUS.loadAll();
        }

        private void btnNhapMoi_Click(object sender, EventArgs e)
        {
            txtMaTheLoai.Clear();
            txtTenTheLoai.Clear();
            btnCapNhat.Enabled = false;
            btnXoa.Enabled = false;
            btnLuu.Enabled = true;
        }
    }
}
